package com.topc.participant.api;

import com.topc.base.api.CommonResult;
import com.topc.base.model.PageParams;
import com.topc.base.model.PageResult;
import com.topc.participant.model.po.CourseInfo;
import com.topc.participant.model.vo.CourseShowVo;
import com.topc.participant.model.vo.PeopleShowVo;
import com.topc.participant.service.ICourseInfoService;
import com.topc.participant.service.ICourseUserMidService;
import com.topc.participant.service.IUserInfoService;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
@RequestMapping("/close/course")
public class CourseCloseController {
    @Autowired
    private ICourseInfoService courseInfoService;
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private ICourseUserMidService courseUserMidService;
    //根据id更新课程状态
    @PostMapping("/update-info")
    public CommonResult<String> updateInfoById(@NotNull Long id, @Range(min = -2,max = 2)  int status){
        courseInfoService.updateInfoById(id,status);
        return CommonResult.success("更新成功");

    }

    //分页查询课程信息
    @PostMapping("/search-info")
    public CommonResult<PageResult<CourseShowVo>> searchInfoByPage(PageParams pageParams, @Range(min = -2,max = 2) int status,@NotNull Long groupId,String courseName){
        PageResult<CourseShowVo> page=courseInfoService.searchByPage(pageParams,status,groupId,courseName);
        return CommonResult.success(page);
    }

    //查询课程中所有人员
    @GetMapping("/get-all-people")
    public CommonResult<List<PeopleShowVo>> getCourseAllPeople(@NotNull Long id){
        List<PeopleShowVo> peopleShowVoList =courseInfoService.getListAllPeople(id);
        return CommonResult.success(peopleShowVoList);
    }

    //新建课程
    @PostMapping("/save-course")
    public CommonResult<String> saveCourse(@RequestBody CourseInfo courseInfo){
        //TODO
//        String tutorNumber = courseInfo.getTutorNumber();
//        String number = SecurityUtil.getUser().getNumber();
//        if (!tutorNumber.equals(number)){
//            Asserts.fail("数据不一致");
//        }
        courseInfoService.saveCourseInfo(courseInfo);
        return CommonResult.success("新建成功");
    }
    //删除课程
    @GetMapping("/delete-course")
    public CommonResult<String> deleteCourse(@NotNull Long courseId,@NotNull String userNumber){
        //TODO
//        String number = SecurityUtil.getUser().getNumber();
//        if (!userNumber.equals(number)){
//            Asserts.fail("您不是该课程的导师，无法删除课程");
//        }
        courseInfoService.deleteCourseInfo(courseId);
        return CommonResult.success("删除成功");
    }

    //修改课程信息
    @PostMapping("/update-course")
    public CommonResult<String> updateCourse(@RequestBody CourseInfo courseInfo){
        //TODO
//        String number = SecurityUtil.getUser().getNumber();
//        if (!courseInfo.getTutorNumber().equals(number)){
//            Asserts.fail("您不是该课程的导师，无法修改课程信息");
//        }
        courseInfoService.updateCourseInfo(courseInfo);
        return CommonResult.success("修改成功");
    }

    //加入课程
    @PostMapping("/add-user-course")
    public CommonResult<String> addUserCourse(@NotNull Long courseId,@NotNull String userNumber){
        courseUserMidService.saveInfo(courseId,userNumber);
        return CommonResult.success("加入成功");
    }
    //退出课程
    @GetMapping("/remove-user-course")
    public CommonResult<String> removeUserCourse(@NotNull Long courseId,@NotNull String userNumber){
        courseUserMidService.removeInfo(courseId,userNumber);
        return CommonResult.success("退出成功");
    }

}
